Page({
  data: {
    boats: [
      {
        id: '1',
        boatNumber: '鲁即渔12375',
        ownerName: '张三',
        idCard: '3702********1234',
        phone: '138****8888'
      },
      {
        id: '2',
        boatNumber: '鲁即渔12376',
        ownerName: '李四',
        idCard: '3702********5678',
        phone: '139****9999'
      }
    ]
  },

  onLoad() {
    this.loadBoats();
  },

  onShow() {
    if (typeof this.getTabBar === 'function' && this.getTabBar()) {
      this.getTabBar().setData({
        selected: 1
      });
    }
  },

  loadBoats() {
    // 加载渔船列表
    wx.request({
      url: 'https://api.example.com/boats',
      method: 'GET',
      success: (res) => {
        if (res.data.success) {
          this.setData({
            boats: res.data.data
          });
        }
      }
    });
  },

  onSearch(e) {
    const keyword = e.detail.value;
    // 搜索渔船
    wx.request({
      url: 'https://api.example.com/boats/search',
      method: 'GET',
      data: {
        keyword
      },
      success: (res) => {
        if (res.data.success) {
          this.setData({
            boats: res.data.data
          });
        }
      }
    });
  },

  onBoatClick(e) {
    const id = e.currentTarget.dataset.id;
    wx.showActionSheet({
      itemList: ['编辑', '解绑'],
      success: (res) => {
        if (res.tapIndex === 0) {
          this.editBoat(id);
        } else if (res.tapIndex === 1) {
          this.unbindBoat(id);
        }
      }
    });
  },

  editBoat(id) {
    wx.navigateTo({
      url: `/pages/boat/edit/index?id=${id}`
    });
  },

  unbindBoat(id) {
    wx.showModal({
      title: '提示',
      content: '确定要解绑该渔船吗？',
      success: (res) => {
        if (res.confirm) {
          wx.request({
            url: 'https://api.example.com/boats/unbind',
            method: 'POST',
            data: {
              id
            },
            success: (res) => {
              if (res.data.success) {
                wx.showToast({
                  title: '解绑成功',
                  icon: 'success'
                });
                this.loadBoats();
              }
            }
          });
        }
      }
    });
  },

  addBoat() {
    wx.navigateTo({
      url: '/pages/boat/add/index'
    });
  },

  goBack() {
    wx.navigateBack();
  }
}); 